﻿using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

    public class GameMapMakerInspectorUI
    {
        int CurTabIndex = 0;
        string[] TabsName = new string[] { "图块管理", "图层", "设置" };

        public GameMapData.GameMapDataHeader EditorHeader = new GameMapData.GameMapDataHeader();

        public void OnInspectorGUI()
        {
            this.CurTabIndex = GUILayout.Toolbar(this.CurTabIndex, TabsName);
           

            switch (this.CurTabIndex)
            {
                case 1:
                    this.DrawLayer();
                break;
                case 2:
                    this.DrawSetting();
                break;
            }
        }


        public void DrawSetting()
        {
            if(GameMapMakerEditor.MapData==null) return;
            GameMapData MapData = GameMapMakerEditor.MapData;

            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("地图名",GUILayout.Width(50));
            EditorHeader.MapName = EditorGUILayout.TextField(EditorHeader.MapName,GUILayout.Width(150));
            GUILayout.EndHorizontal();
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("地图路径", GUILayout.Width(50));
            EditorHeader.MapPath = EditorGUILayout.TextField(EditorHeader.MapPath, GUILayout.Width(230));
            GUILayout.EndHorizontal();
            
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("地图宽度",GUILayout.Width(50));
            EditorHeader.MapWidth = EditorGUILayout.IntField(EditorHeader.MapWidth,GUILayout.Width(50));
            GUILayout.Space(30);
            EditorGUILayout.LabelField("地图高度", GUILayout.Width(50));
            EditorHeader.MapHeight = EditorGUILayout.IntField(EditorHeader.MapHeight, GUILayout.Width(50));
            GUILayout.EndHorizontal();
            if (EditorHeader.MapWidth < MapData.Header.MapWidth || EditorHeader.MapHeight < MapData.Header.MapHeight)
            {
                EditorGUILayout.HelpBox("新地图尺寸长或高小于旧地图尺寸,修改后部分数据将丢失.", MessageType.Warning);
            }
            

            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("图块宽度", GUILayout.Width(50));
            EditorHeader.TileSizeX= EditorGUILayout.IntField(EditorHeader.TileSizeX, GUILayout.Width(50));
            GUILayout.Space(30);
            EditorGUILayout.LabelField("图块高度", GUILayout.Width(50));
            EditorHeader.TilesizeY = EditorGUILayout.IntField(EditorHeader.TilesizeY, GUILayout.Width(50));
            GUILayout.EndHorizontal(); 
             
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("描述",GUILayout.Width(50));
            EditorHeader.MapDesc = EditorGUILayout.TextArea(EditorHeader.MapDesc,GUILayout.Height(80));
            GUILayout.EndHorizontal();

            GUILayout.Space(10);
            Color oldColor = GUI.backgroundColor;
            GUI.color = Color.green;
            if (GUILayout.Button("确定修改")) 
            {
                if (MapData == null)
                {
                    return;
                }

                ChangeMapSetting(MapData, EditorHeader);
                GameMapEditorPanel.Inst.Load();
                GameMapFileManager.Inst.SaveGameMap(MapData);
            }
            GUI.backgroundColor = oldColor;
        }

        void ChangeMapSetting(GameMapData mapData,GameMapData.GameMapDataHeader newHeader)
        {
            
            //new blockData
            GameMapData.MapLayerData[] newlayerDatas = new GameMapData.MapLayerData[mapData.LayerDatas.Length];
            for (int i=0;i<newlayerDatas.Length;i++)
            {
                newlayerDatas[i] = new GameMapData.MapLayerData();
                newlayerDatas[i].mapData = mapData;
                newlayerDatas[i].Id = mapData.LayerDatas[i].Id;
                newlayerDatas[i].Desc = mapData.LayerDatas[i].Desc;
                newlayerDatas[i].MapBlocks = new MapBlock[newHeader.MapHeight,newHeader.MapWidth];
                //copyBlockData
                copyOldBockData(newlayerDatas[i],mapData.LayerDatas[i],newHeader,mapData.Header);

            }
            mapData.LayerDatas = newlayerDatas;
            mapData.Header = new GameMapData.GameMapDataHeader(newHeader);
        }
        void copyOldBockData(GameMapData.MapLayerData newData,GameMapData.MapLayerData oldData,
            GameMapData.GameMapDataHeader newHeader, GameMapData.GameMapDataHeader oldHeader)
        {
            for (int y = 0; y < newHeader.MapHeight; y++)
            {
                for (int x = 0; x < newHeader.MapWidth; x++)
                {
                    newData.MapBlocks[y, x] = null;
                }
            }

            for (int y = 0; y < oldHeader.MapHeight; y++)
            {
                for (int x = 0; x < oldHeader.MapWidth; x++)
                {
                    if (y < newHeader.MapHeight && x < newHeader.MapWidth)
                    {
                        MapBlock block =  oldData.MapBlocks[y, x];
                        if (block == null)
                        {
                            newData.MapBlocks[y, x] = null;
                        }
                        else if(block.PosX== x&&block.PosY==y)
                        {
                            newData.AddBlock(block.StaticBlockIndex,x,y);
                        }
                    }
                }
            }
        }

        public void DrawLayer()
        {
            GUILayout.Label("选择当前编辑的层");
            GUILayout.Space(5);
            GameMapData mapData = GameMapMakerEditor.MapData;
            if (GameMapMakerEditor.MapData == null) return;
            for (int i=0;i<mapData.LayerDatas.Length;i++)
            {
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Toggle(mapData.CurEditLayout == i, mapData.LayerDatas[i].Desc))
                {
                    mapData.CurEditLayout = i;
                }
                EditorGUILayout.EndHorizontal();
                GUILayout.Space(10);
            }
        }

        

    }
